<template>
  <div>
    <el-dialog
      v-model="open"
      :title="isEdit"
      width="50%"
      :close-on-click-modal="false"
      :inline="false"
      append-to-body
    >
      <el-form
        ref="editForm"
        :model="form"
        :rules="rules"
        label-width="120px"
        class="demo-form"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称：" prop="fuelCode">
              <el-input v-model="form.fuelCode"></el-input>
            </el-form-item>
            <el-form-item label="生效时间:" required>
              <el-col :span="11">
                <el-form-item prop="effectiveDate">
                  <el-date-picker
                    v-model="form.effectiveDate"
                    type="date"
                    placeholder="选择日期"
                    style="width: 100%;"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item label="燃油种类中文:" label-width="120px" prop="fuelTypeChinese">
              <el-input v-model="form.fuelTypeChinese"></el-input>
            </el-form-item>;
            <el-form-item label="燃油系数:" prop="fuelCoefficient">
              <el-input-number
                v-model="form.fuelCoefficient"
                :precision="3"
                :step="0.1"
                :max="2"
              ></el-input-number>
            </el-form-item>
            <div class="grid-content bg-purple"></div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light">
              <el-form-item label="状态：" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio :label="0">生效</el-radio>
                  <el-radio :label="1">失效</el-radio>
                  <el-radio :label="2">草稿</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="失效时间：" required>
                <el-col :span="11">
                  <el-form-item prop="expirationDate">
                    <el-date-picker
                      v-model="form.expirationDate"
                      type="date"
                      placeholder="选择日期"
                      style="width: 100%;"
                    ></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-form-item>
              <el-form-item label="燃油种类英文：" label-width="120px" prop="fuelTypeEnglish">
                <el-input v-model="form.fuelTypeEnglish"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="saveHandler">确定</el-button>
          <el-button @click="closeHandler">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, defineExpose, getCurrentInstance } from 'vue'
import { ElMessage, ElMessageBox, ElButton } from 'element-plus'
import { resetForm } from '@/utils'
import { addFuelList, editFuelList } from '@/api/BusinessConfig/fuelMaintain'

const emit = defineEmits(['changeList'])
// 表单状态
let open = ref(false),
  isEdit = ref(null),
  // 表单参数
  form = ref({
    id: undefined,
    fuelCode: undefined,
    effectiveDate: undefined,
    fuelTypeChinese: undefined,
    fuelCoefficient: 0.0,
    status: undefined,
    expirationDate: undefined,
    menuCheckStrictly: undefined,
    fuelTypeEnglish: undefined,
  }),
  // 规则
  rules = {
    fuelCode: [{ required: true, message: '请输入编码', trigger: 'blur' }],
    effectiveDate: [{ required: true, message: '请选择生效时间', trigger: 'change' }],
    fuelTypeChinese: [{ required: true, message: '请输入燃油种类中文', trigger: 'blur' }],
    fuelCoefficient: [{ required: true, message: '请选择燃油系数', trigger: 'change' }],
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
    expirationDate: [{ required: true, message: '请选择失效时间', trigger: 'change' }],
    fuelTypeEnglish: [{ required: true, message: '请输入燃油种类英文', trigger: 'blur' }],
  },
  editForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()
// 重置表单
const reset = () => {
  form.value = {
    id: undefined,
    fuelCode: undefined,
    effectiveDate: undefined,
    fuelTypeChinese: undefined,
    fuelCoefficient: 0.0,
    status: undefined,
    expirationDate: undefined,
    menuCheckStrictly: undefined,
    fuelTypeEnglish: undefined,
  }
  $resetForm(editForm.value)
}
// 新增-修改表单
const saveHandler = () => {
  $validateForm(editForm.value, () => {
    let msg = ''
    let response = null
    if (form.value.id != undefined) {
      msg = '修改成功'
      response = editFuelList(form.value)
    } else {
      msg = '新增成功'
      response = addFuelList(form.value)
    }
    response
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success(msg)
        open.value = false
        emit('changeList')
        reset()
      } else {
        ElMessage.error(res.msg)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
      reset()
    })
  })
}
// 取消新增-修改
const closeHandler = () => {
  open.value = false
  reset()
}

defineExpose({
  open,
  isEdit,
  form,
})
</script>
